package mobi.imzw.control{
	import flash.display.Shape;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	
	public class PolylineDescriptor extends Shape {
		
		private var _hasArrowHead:Boolean=false;
		
		/////////////////////////////////////////
		//线的样式
		//////////////////////////////////////////
		private var _thinkness:Number = 1;
		private var _color:uint = 0x000000;
		private var _alpha:Number = 1;
		private var _pixelHinting:Boolean = false;
		private var _scaleMode:String = "normal";
		private var _points:ArrayCollection;
		
		public function PolylineDescriptor(thePoints:ArrayCollection=null,
										hasArrow:Boolean=false){
			points = thePoints;
			hasArrowHead = hasArrow;
		}
		
		/**
		 * 划线
		 * */
		public function draw():void{
			graphics.clear();
			with(graphics){
				lineStyle(_thinkness, _color, _alpha, _pixelHinting, _scaleMode);
				
				for(var i:int=0; i<points.length; i++){
					var p:Point = Point(points.getItemAt(i));
					//moveTo(p.x, p.y);
					lineTo(p.x, p.y);
				}
			}
			
			if(hasArrowHead){
				drawArrowHead();
			}
		}
		
		/**
		 * 画箭头
		 * draw arrow for line 
		 **/
		private function drawArrowHead():void{
			
			var startX:Number = Point(points.getItemAt(0)).x;
			var startY:Number =  Point(points.getItemAt(0)).y;
			if(points.length > 1){
				startX = Point(points.getItemAt(points.length-2)).x;
				startY = Point(points.getItemAt(points.length-2)).y;
			}
			var endX:Number =  Point(points.getItemAt(points.length-1)).x;
			var endY:Number =  Point(points.getItemAt(points.length-1)).y;
			var arrowLength : Number = 10;
			var arrowAngle : Number = Math.PI / 6;
			var lineAngle : Number;
			if(endX - startX != 0)				
				lineAngle = Math.atan((endY - startY) / (endX - startX));
			else{
				if(endY - startY < 0)
					lineAngle = Math.PI / 2;
				else
					lineAngle = 3 * Math.PI / 2;
			}				
			if(endY - startY >= 0 && endX - startX <= 0){
				lineAngle = lineAngle + Math.PI;
			}else if(endY - startY <= 0 && endX - startX <= 0){
				lineAngle = lineAngle + Math.PI;
			}
			//定义三角形
			var angleC : Number = arrowAngle;
			var rimA : Number = arrowLength;
			var rimB : Number = Math.pow(Math.pow(endY - startY,2) + Math.pow(endX - startX,2),1/2);
			var rimC : Number = Math.pow(Math.pow(rimA,2) + Math.pow(rimB,2) - 2 * rimA * rimB * Math.cos(angleC),1/2);
			var angleA : Number = Math.acos((rimB - rimA * Math.cos(angleC)) / rimC);
			
			var leftArrowAngle : Number = lineAngle + angleA;
			var rightArrowAngle : Number = lineAngle - angleA;			
			var leftArrowX : Number = startX + rimC * Math.cos(leftArrowAngle);
			var leftArrowY : Number = startY + rimC * Math.sin(leftArrowAngle);			
			var rightArrowX : Number = startX + rimC * Math.cos(rightArrowAngle);
			var rightArrowY : Number = startY + rimC * Math.sin(rightArrowAngle);
			
			
			with(graphics){
				moveTo(endX, endY);
				lineTo(leftArrowX, leftArrowY);
				moveTo(endX, endY);
				lineTo(rightArrowX, rightArrowY);
			}
		}
		
		/**
		 * 是否有箭头
		 **/
		public function get hasArrowHead():Boolean{
			return _hasArrowHead;
		}
		public function set hasArrowHead(value:Boolean):void{
			_hasArrowHead = value;
		}
		
		/**
		 * 线的起点
		 **/
		public function get points():ArrayCollection{
			return _points;
		}
		public function set points(value:ArrayCollection):void{
			if(!value){
				_points = new ArrayCollection();
				_points.addItem(new Point(0, 0));
			}else{
				_points = value;
			}
		}
	}
}